<?php

/**
 * Created by PhpStorm.
 * User: zhangxiang
 * Date: 2017/6/2 0002
 * Time: 下午 4:34
 */
class ArticleListCacheModel
{
    private $redisModel;
    private $module = 'article';

    function __construct() {
        $this->redisModel    =    new RedisModel();
    }

    /**
     * 获取文章缓存
     * @param $articleBodyId
     * @return bool
     */
    public function getArticleCache($articleBodyId){
        if(empty($articleBodyId)) return FALSE;
        $hashKey = CacheConf::getCacheKeyDetail($this->module, 'hash', $articleBodyId);
        return $this->getHashCache($hashKey);
    }

    /**
     * 增加文章缓存
     * @param $articleBodyId
     * @param $data
     * @return bool
     */
    public function addArticleCache($articleBodyId, $data){
        if (AC::REDIS_WRITE){
            isset($data['attachment']) && $data['attachment'] = json_encode($data['attachment']);
            isset($data['modules']) && $data['modules'] = json_encode($data['modules']);
            isset($data['extras']) && $data['extras'] = json_encode($data['extras']);

            $cacheKey = CacheConf::getCacheKeyDetail($this->module, '', $articleBodyId);
            $this->setHashCache($cacheKey['hash'], $cacheKey['sset'], $cacheKey['expire'], $data, $articleBodyId);
        }
        return true;
    }

    /**
     * 删除缓存
     * @param $articleBodyId
     * @return bool|mixed
     */
    public function delArticleCache($articleBodyId){
        if(empty($articleBodyId)) return FALSE;
        $cacheKey = CacheConf::getCacheKeyDetail($this->module, '', $articleBodyId);
        return $this->detHashCache($cacheKey['hash'], $cacheKey['sset']);
    }


    public function setArticleTimedList($bodyId, $time) {
        if (AC::REDIS_WRITE) {
            return $this->redisModel->setArticleTimedList($bodyId, $time);
        }
    }



    public function getHashCache($hash_key){
        if (AC::REDIS_READ){
            return $this->redisModel->getHashCache($hash_key);
        }
    }

    public function setHashCache($hash_key, $sset_key, $expire, $data, $key){
        if (AC::REDIS_WRITE){
            return $this->redisModel->setHashCache($hash_key, $sset_key, $expire, $data, $key);
        }
    }

    public function detHashCache($hash_key, $sset_key){
        if (AC::REDIS_WRITE){
            return $this->redisModel->detHashCache($hash_key, $sset_key);
        }
    }

}